#include "PlayersStatsShowMenu.h"
#include "Utilities.h"
#include "PrintUtilities.h"
#include "MenuManager.h"
#include "ServiceProvider.h"
#include "PlayerManager.h"
#include "GamePrintUtils.h"
#include "Player.h"
#include "PlayerFunctors.h"

#include <iostream>

PlayersStatsShowMenu::PlayersStatsShowMenu(MenuManager* const i_menuManager)
	:Menu(i_menuManager)
{
}

void PlayersStatsShowMenu::Draw() const
{
	using namespace std;

	cout << Print::Title("PLAYERS STATISTICS:");
	cout << " 1) Sort by Age Up" << endl;
	cout << " 2) Sort by Age Down" << endl;
	cout << " 3) Sort by Num Seasons" << endl;
	cout << " 4) Sort by Num Injuries" << endl;
	cout << " 5) Sort by Salary" << endl;
	cout << " 6) Sort by Goalkeaping" << endl;
	cout << " 7) Sort by Defense" << endl;
	cout << " 8) Sort by Attacking" << endl;
	cout << " 9) Sort by Resistence" << endl;
	cout << "10) Sort by Season Played Matches" << endl;
	cout << "11) Sort by Season Goals" << endl;
	cout << "12) Sort by Total Played Matches" << endl;
	cout << "13) Sort by Total Goals" << endl;
	cout << "14) Sort by Total Goal Ratio" << endl;
	cout << endl << endl;

	ShowPlayers();

	string options = "1-14) Select Sorting Criterion.\n\n0) Return to previous menu'.";
	cout << Print::Options(options) << "Selection: ";
}

void PlayersStatsShowMenu::ShowPlayers() const
{
	using std::cout; using std::endl;
	const int maxPlayerNameSize = Player::GetPlayerParams().MAX_CHAR_PLAYER_NAME - 5;

	cout << Print::TabUpTo("NAME", maxPlayerNameSize);
	cout << Print::TabUpTo("TEAM", 10);
	cout << Print::TabUpTo("YS", 3);
	cout << Print::TabUpTo("NS", 3);
	cout << Print::TabUpTo("ATK", 4);
	cout << Print::TabUpTo("DEF", 4);
	cout << Print::TabUpTo("GLK", 4);
	cout << Print::TabUpTo("RES", 4);
	cout << Print::TabUpTo("SAL", 4);
	cout << Print::TabUpTo("NM", 3);
	cout << Print::TabUpTo("GL", 3);
	cout << Print::TabUpTo("NMT", 4);
	cout << Print::TabUpTo("GLT", 5);
	cout << Print::TabUpTo("RAT", 5);
	cout << Print::TabUpTo("NI", 3);
	cout << endl;

	size_t maxSize = std::min<size_t>(Player::NUM_HOF_PLAYERS, m_players.size());
	for(size_t i=0; i<maxSize; ++i)
	{
		const Player& thePlayer = *m_players.at(i);
		cout << Print::TabUpTo(thePlayer.GetName().substr(0, maxPlayerNameSize-1), maxPlayerNameSize);
		cout << Print::TabUpTo(thePlayer.GetTeamName().substr(0,9), 10);
		cout << Print::TabUpTo(Utilities::IntToString(thePlayer.GetAge()), 3);
		cout << Print::TabUpTo(Utilities::IntToString(thePlayer.GetNumSeason()), 3);
		cout << Print::TabUpTo(Utilities::IntToString(thePlayer.GetSkill(Attacker)), 4);
		cout << Print::TabUpTo(Utilities::IntToString(thePlayer.GetSkill(Defender)), 4);
		cout << Print::TabUpTo(Utilities::IntToString(thePlayer.GetSkill(Goalkeeper)), 4);
		cout << Print::TabUpTo(Utilities::IntToString(thePlayer.GetSkills().m_resistance), 4);
		cout << Print::TabUpTo(Utilities::IntToString(thePlayer.GetSalary()), 4);
		cout << Print::TabUpTo(Utilities::IntToString(thePlayer.GetPlayedMatch()), 3);
		cout << Print::TabUpTo(Utilities::IntToString(thePlayer.GetScoredGoal()), 3);
		cout << Print::TabUpTo(Utilities::IntToString(thePlayer.GetTotalPlayedMatch()), 4);
		cout << Print::TabUpTo(Utilities::IntToString(thePlayer.GetTotalScoredGoal()), 5);
		cout << Print::TabUpTo(thePlayer.GetTotalScoredGoalRatioStr(), 5);
		cout << Print::TabUpTo(Utilities::IntToString(thePlayer.GetNumOfInjury()), 3);
		cout << endl;
	}
	cout << endl;
}

bool PlayersStatsShowMenu::Input(const std::string& i_input)
{
	bool goodInput = true;
	const int res = Utilities::StringToInt(i_input);

	switch(res)
	{
	case 0:
		GetMM()->SetMenu(GetMM()->GetPlayersStatsMenu());
		break;
	case 1:
		std::sort(m_players.begin(), m_players.end(), PlayerComparisionAge);
		break;
	case 2:
		std::sort(m_players.begin(), m_players.end(), PlayerComparisionAgeDown);
		break;
	case 3:
		std::sort(m_players.begin(), m_players.end(), PlayerComparisionNumSeason);
		break;
	case 4:
		std::sort(m_players.begin(), m_players.end(), PlayerComparisionNumInjury);
		break;
	case 5:
		std::sort(m_players.begin(), m_players.end(), PlayerComparisionSalary);
		break;
	case 6:
		std::sort(m_players.begin(), m_players.end(), PlayerComparisionSkill(Goalkeeper));
		break;
	case 7:
		std::sort(m_players.begin(), m_players.end(), PlayerComparisionSkill(Defender));
		break;
	case 8:
		std::sort(m_players.begin(), m_players.end(), PlayerComparisionSkill(Attacker));
		break;
	case 9:
		std::sort(m_players.begin(), m_players.end(), PlayerComparisionResistance);
		break;
	case 10:
		std::sort(m_players.begin(), m_players.end(), PlayerComparisionMatch);
		break;
	case 11:
		std::sort(m_players.begin(), m_players.end(), PlayerComparisionGoal);
		break;
	case 12:
		std::sort(m_players.begin(), m_players.end(), PlayerComparisionTotalMatch);
		break;
	case 13:
		std::sort(m_players.begin(), m_players.end(), PlayerComparisionTotalGoal);
		break;
	case 14:
		std::sort(m_players.begin(), m_players.end(), PlayerComparisionTotalGoalRatio);
		break;
	default:
		goodInput = false;
		break;
	};

	return goodInput;
}



